package com.eqdd.yiqidian.ui.discover.makerspace

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows
import com.eqdd.yiqidian.bean.rebuild.LearnArticleListBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.http.JsonConverter
import com.eqdd.yiqidian.ui.yiqiyue.ArticleInfoActivity
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import com.lzy.okrx2.adapter.FlowableBody
import com.shizhefei.mvc.MVCCoolHelper
import io.reactivex.Flowable
import kotlinx.android.synthetic.main.activity_recyclerview_refresh.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.util.*

/**
 * Created by jhj on 18-8-3.
 */
class MakerDiaryListActivity : BaseActivity() {

    private var mvcHelper: MVCCoolHelper<List<LearnArticleListBean>>? = null
    var pageNum: Int = 0

    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview_refresh)
        initTopTitleBar(View.VISIBLE, "日志列表")
    }

    override fun initData() {
        val list = arrayListOf<LearnArticleListBean>()
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        val slimAdapterEx = SlimAdapterEx.create()
                .register<LearnArticleListBean>(R.layout.list_item_178, SlimInjector<LearnArticleListBean> { data, injector ->
                    injector?.image(R.id.iv_head, data?.iphoto)
                            ?.text(R.id.stv_name, data?.upname)
                            ?.text(R.id.tv_depart, data?.createTime)
                            ?.gone(R.id.iv_option)
                            ?.image(R.id.iv_img, data?.image)
                            ?.text(R.id.tv_title, data?.title)
                            ?.text(R.id.tv_content, data?.textContent)
                            ?.visibility(R.id.iv_img, if (TextUtils.isEmpty(data?.image?.trim { it <= ' ' })) View.GONE else View.VISIBLE)
                            ?.text(R.id.tv_source, "创客空间")
                            ?.text(R.id.tv_count, "阅读:" + data?.browseCount)
                            ?.with<RecyclerView>(R.id.recyclerViewLabel) { view ->
                                if (data?.label == null) {
                                    return@with
                                }
                                val stringList = ArrayList<String>()
                                for (s in data.label.split(",".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
                                    if (!TextUtils.isEmpty(s)) {
                                        stringList.add(s)
                                    }
                                }
                                view.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
                                SlimAdapterEx.create()
                                        .register<String>(R.layout.list_item_74, SlimInjector<String> { data, injector ->
                                            injector?.gone(R.id.iv_clear)
                                                    ?.text(R.id.tv_content, data)
                                        }).attachTo(view).updateData(stringList as List<String>?)

                            }

                })
                .attachTo(recyclerView)
                .updateData(list as List<LearnArticleListBean>?) as SlimAdapterEx<LearnArticleListBean>?
        ItemClickSupport.addTo(recyclerView).setOnItemClickListener { recyclerView, position, v ->
            val bean = slimAdapterEx?.getDataItem(position)
            startActivity<ArticleInfoActivity>(Config.ID to bean?.id)
        }

        val dataSource = ModelRx2DataSource<LearnArticleListBean>(ModelRx2DataSource.OnLoadSource<LearnArticleListBean> { page, register ->
            pageNum = if (page == 1) 0 else pageNum
            OkGo.post<HttpResultRows<LearnArticleListBean>>(HttpConfig.BASE_URL + HttpConfig.YUE_USER_ARTICLE_LIST)
                    .params("userGuid", user.guid)
                    .params("page", pageNum)
                    .converter(object : JsonConverter<HttpResultRows<LearnArticleListBean>>() {
                        override fun test() {
                            super.test()
                        }
                    })
                    .adapt(FlowableBody<HttpResultRows<LearnArticleListBean>>())
                    .flatMap({ result ->
                        if (page == 1) {
                            mvcHelper?.isAutoLoadMore = true
                        }
                        pageNum = result.items.page
                        Flowable.just<MutableList<LearnArticleListBean>>(
                                if (result.items == null)
                                    ArrayList()
                                else
                                    result.items.rows as MutableList<LearnArticleListBean>?)
                    })


        }, 10)

        mvcHelper = MVCCoolHelper<List<LearnArticleListBean>>(coolRefreshView)
        mvcHelper?.setDataSource(dataSource)
        mvcHelper?.adapter = slimAdapterEx
        mvcHelper?.refresh()

    }

    override fun setView() {
    }
}